package com.aizhixin.lab.resource.service;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.course.course.entity.CourseChapterAtt;
import com.aizhixin.lab.course.course.repository.CourseChapterAttRepository;
import com.aizhixin.lab.resource.domain.FileInfoDomain;
import com.aizhixin.lab.task.domain.QueryDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class VideoService {
    @Autowired
    private CourseChapterAttRepository courseChapterAttRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;

    RowMapper<FileInfoDomain> attFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            domain.setId(rs.getString("table_name")+"_" + rs.getLong("ID"));
            domain.setFileName(rs.getString("file_name"));
            Long parenId = rs.getLong("parent_id");
            if (null != parenId && 0 != parenId) {
                domain.setChapterName(rs.getString("chapter_name"));
                domain.setMinutiaName(rs.getString("minutia_name"));
            } else {
                domain.setChapterName(rs.getString("minutia_name"));
            }
            domain.setCourseName(rs.getString("course_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };
    public <T> Map<String, Object> findVideoFile(QueryDomain domain){
        String sql="SELECT * FROM (SELECT cccf.id,cccf.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date,'C' table_name,cccf.delete_flag,cc.id course_id FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id WHERE cccf.delete_flag=0 and cccf.type =20 UNION ALL SELECT ccca.id,ccca.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,ccca.created_date,'E' table_name,ccca.delete_flag,cc.id course_id FROM c_course_chapter_att ccca LEFT JOIN c_course_chapter ccc1 ON ccca.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id=ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id where ccca.type=20) a ";
        String countSql="SELECT count(*) FROM (SELECT cccf.id,cccf.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date,'C' table_name,cccf.delete_flag FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id WHERE cccf.type =20 UNION ALL SELECT ccca.id,ccca.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,ccca.created_date,'E' table_name,ccca.delete_flag FROM c_course_chapter_att ccca LEFT JOIN c_course_chapter ccc1 ON ccca.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id=ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id where ccca.type=20) a ";
        if (!StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and a.course_id= " + s[1];
            countSql += " and a.course_id= " + s[1];
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("a.created_date");
        sort.add(dto);
        return pageJdbcUtil.getPageInfor(domain.getPageSize(),domain.getPageNumber(),attFileRm,sort,sql,countSql) ;
    }
    public CourseChapterAtt findAttFile(Long id){
       return courseChapterAttRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
    }
    public void deleteAttFile(Long id){
        CourseChapterAtt chapterAtt = courseChapterAttRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
        if (chapterAtt!=null){
            chapterAtt.setDeleteFlag(DataValidity.INVALID.getState());
            courseChapterAttRepository.save(chapterAtt);
        }
    }
    public CourseChapterAtt updateAttFileName(Long id,String name){
        CourseChapterAtt chapterAtt = courseChapterAttRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
        if (chapterAtt!=null){
            chapterAtt.setName(name);
          return   courseChapterAttRepository.save(chapterAtt);
        }
        return null;
    }
}
